package filtros;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Pixel {


	public static ArrayList<Double> rgbToYiqConversion(int argb) {
		
		int R = Pixel.getRed(argb);
		int G = Pixel.getGreen(argb);
		int B = Pixel.getBlue(argb);
		
		double Y = (0.299*R + 0.587*G + 0.114*B);
		double I = (0.596*R - 0.274*G - 0.322*B);
		double Q = (0.211*R - 0.523*G + 0.312*B);
		
		ArrayList<Double> yiqDots = new ArrayList<Double>();
		yiqDots.add(Y);
		yiqDots.add(I);
		yiqDots.add(Q);
		
		return yiqDots;
	}
	

	public static int yiqToRgbConversion(ArrayList<Double> yiqDots) {
		
		double Y = yiqDots.get(0);
		double I = yiqDots.get(1);
		double Q = yiqDots.get(2);
		
		int R = (int) Math.round(Y + 0.956*I + 0.621*Q);
		int G = (int) Math.round(Y - 0.272*I - 0.647*Q);
		int B = (int) Math.round(Y - 1.106*I + 1.703*Q);

		if(R < 0){
			R = 0;
		}else if(R > 255) {
			R = 255;
		}
		
		if(G < 0){
			G = 0;
		}else if(G > 255) {
			G = 255;
		}
		
		if(B < 0){
			B = 0;
		}else if(B > 255) {
			B = 255;
		}
		
		return Pixel.setRGB(R, G, B, (R+B+G)/3);
	}
	
		
	public static int getRed(int ARGB) {
		return ((ARGB & (0x00FF0000)) >> 16);
	}
	
	public static int getGreen(int ARGB) {
		return  ((ARGB & (0x0000FF00)) >> 8);
	}
	
	public static int getBlue(int ARGB) {
		return ((ARGB & (0x000000FF)) >> 0);
	}
	
	public static int setRGB(int R, int G, int B, int ARGB){
		int resultado = 0 ;
		resultado += ARGB & 0xFF000000;
		resultado += R << 16;
		resultado += G << 8;
		resultado += B << 0;
		
		return resultado;
	}
}
